//
//  ThemeViewController.m
//  Qyer
//
//  Created by 赵宇 on 15/9/16.
//  Copyright (c) 2015年 赵宇. All rights reserved.
//

#import "ThemeViewController.h"
#import "BaseHeader.h"
@interface ThemeViewController ()<UITableViewDataSource,UITableViewDelegate,UIScrollViewDelegate>

@end

@implementation ThemeViewController
-(void)dealloc
{
    [_url release];
    [_poisArr release];
    [_infoArr release];
    [_restaurantArr release];
    [_navView release];
    [_table release];
    [super dealloc];
}
-(void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBarHidden = YES;
    

}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _poisArr = [[NSMutableArray alloc]init];
    self.navigationController.automaticallyAdjustsScrollViewInsets = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    self.page = 1;
    
    [self getTheData:1];
}
-(void)getTheData:(int)num
{
    _infoArr = [[NSMutableArray alloc]init];
    
    _restaurantArr = [[NSMutableArray alloc]init];
    
    NSString *urlStr = [NSString stringWithFormat:@"http://open.qyer.com/qyer/footprint/mguide_detail?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=10&id=%@&oauth_token=d5ecb9febb61510d54ba2d47d986cf65&page=%d&track_app_version=6.6.6&track_user_id=6695170&v=1",self.url,num];
    [AFNblock getdataWithUrlString:urlStr dataBlock:^(id block) {
        
        [self dealDataWithDic:block];
    } netBlock:^(id block) {
        [self dealDataWithDic:block];
        
    }];
}
-(void)dealDataWithDic:(NSDictionary *)block
{
    NSDictionary *bigDic = block;
    NSDictionary *dataDic = [bigDic objectForKey:@"data"];
    SmallStrategyModel *datamol = [[SmallStrategyModel alloc]initWithDictionary:dataDic];
    [_infoArr addObject:datamol];
    
    NSArray *poisArr = [dataDic objectForKey:@"pois"];
    for (int i = 0; i < poisArr.count; i++) {
        NSDictionary *dictionary = [poisArr objectAtIndex:i];
        PoisModel *poismol = [[PoisModel alloc]initWithDictionary:dictionary];
        [_poisArr addObject:poismol];
        NSString *cid = poismol.cid;
        [_restaurantArr addObject:cid];
    }
    
    if (_table == nil) {
        
        [self creatTableview];
    }
//    [self navigation];
    [_table.footer endRefreshing];
    [_table reloadData];
    

}
-(void)navigation
{
    _navView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 64)];
    [_navView setBackgroundColor:[UIColor colorWithRed:.98 green:.98 blue:.95 alpha:1]];
    _navView.alpha = 0;
    
    
   UIButton *backBut = [UIButton buttonWithType:UIButtonTypeCustom];
    [backBut setImage:[UIImage imageNamed:@"whiteback.png"] forState:UIControlStateNormal];
    backBut.frame = CGRectMake(10, 30, 24, 24);
    [backBut addTarget:self action:@selector(backViewClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backBut];
    
    UIButton *back = [UIButton buttonWithType:UIButtonTypeCustom];
    [back setImage:[UIImage imageNamed:@"iconfont-31fanhui1"] forState:UIControlStateNormal];
    back.frame = CGRectMake(10, 30, 24, 24);
    [back addTarget:self action:@selector(backViewClick:) forControlEvents:UIControlEventTouchUpInside];
    [_navView addSubview:back];

    [self.view addSubview:_navView];

    
}
#pragma mark - 点击 < 回到上一页
-(void)backViewClick:(UIButton *)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView == _table) {
        
        if (scrollView.contentOffset.y > 0) {
        
            _navView.alpha = 0.01 * scrollView.contentOffset.y;
        }
    }
    
}
#pragma mark - 创建tableView
-(void)creatTableview
{
    UIImageView *headView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT/5*3-10)];
    headView.userInteractionEnabled = YES;
    UIImageView *headPic = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT/3)];

    SmallStrategyModel *headMod = [_infoArr objectAtIndex:0];
    
    [headPic setUserInteractionEnabled:YES];
    
    NSURL *url = [NSURL URLWithString:headMod.photo];
    [headPic sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"zhanweiPic"]];
    [headView addSubview:headPic];
    
    UILabel *classLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT/5, SCREEN_WIDTH- 20, SCREEN_HEIGHT/5)];
    NSString *string = [NSString stringWithFormat:@"---目的地主题推荐---"];
    classLabel.text = string;
    classLabel.textAlignment = YES;
    classLabel.center  = headPic.center;
    classLabel.textColor = [UIColor whiteColor];
    classLabel.font = [UIFont boldSystemFontOfSize:20.];
    [headPic addSubview:classLabel];
    
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, headPic.frame.size.height + 15, SCREEN_WIDTH, 40)];
    titleLabel.textAlignment = YES;
    titleLabel.text = headMod.title;
    titleLabel.numberOfLines = 2;
    [headView addSubview:titleLabel];
    
    
    UILabel *desLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, headPic.frame.size.height+55, SCREEN_WIDTH - 20, SCREEN_HEIGHT*0.11)];
    desLabel.font = [UIFont systemFontOfSize:13.];
    desLabel.numberOfLines = 0;
    desLabel.textColor = [UIColor grayColor];
    desLabel.text = headMod.Mydescription;
    CGFloat hei = [StringWithHeight HeightWithText:desLabel.text width:SCREEN_WIDTH - 20 font:[UIFont systemFontOfSize:13.]];
    desLabel.frame = CGRectMake(10, headPic.frame.size.height+55, SCREEN_WIDTH - 20, hei);
    [headView addSubview:desLabel];
    
    headView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT/3+65+hei);
    _table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStylePlain];
    _table.tableHeaderView = headView;
    [_table setDelegate:self];
    [_table setDataSource:self];
    
    [self shangla];
    [self.view addSubview:_table];
    [self navigation];
}
#pragma mark tableView代理方法的实现
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _poisArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *themeCellid = @"themeViewCellID";
    ThemeTableViewCell *themeCell = [tableView dequeueReusableCellWithIdentifier:themeCellid];
    if (!themeCell) {
        themeCell = [[ThemeTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:themeCellid];
    }
    themeCell.selectionStyle = UITableViewCellSelectionStyleNone;
    PoisModel *poismod = [_poisArr objectAtIndex:indexPath.row];
    [themeCell setValueWithModel:poismod];
    
    CGFloat hei = [StringWithHeight HeightWithText:themeCell.descriptionLabel.text width:SCREEN_WIDTH - 20 font:[UIFont systemFontOfSize:13.]];
    themeCell.descriptionLabel.numberOfLines = 0;
    themeCell.descriptionLabel.frame = CGRectMake(10, SCREEN_HEIGHT/4+15, SCREEN_WIDTH - 20, hei);

    _wid = hei;
    return themeCell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return SCREEN_HEIGHT/4+_wid+30;
}

-(void)shangla
{
    __block ThemeViewController *theme = self;
    [_table addLegendFooterWithRefreshingBlock:^{
        theme.page += 1;
        [theme getTheData:theme.page];
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
